﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Library;
using FileExpert.MP4;


namespace FileExpert
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void openMP4FileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open a file.
            OpenFileDialog openStreamFileDialog = new OpenFileDialog();
            openStreamFileDialog.Filter = "MP4 file(*.mp4)|*.mp4|All files(*.*)|*.*";
            openStreamFileDialog.Title = "Select a MP4 file";
            openStreamFileDialog.CheckFileExists = true;
            openStreamFileDialog.CheckPathExists = true;
            if (DialogResult.OK == openStreamFileDialog.ShowDialog())
            {
                OpenLocalFile(openStreamFileDialog.FileName);
            }
        }

        private void tileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            List<string> recentFileList = ManagerMRU.GetMruList();

            toolStripMenuItemRecentFiles.DropDownItems.Clear();

            foreach (string file in recentFileList)
            {
                ToolStripMenuItem fileMenu = new ToolStripMenuItem(file);
                toolStripMenuItemRecentFiles.DropDownItems.Add(fileMenu);
                fileMenu.Click += new System.EventHandler(this.OpenRecentFile_Click); ;
            }
        }

        private void OpenRecentFile_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
            OpenLocalFile(menuItem.Text);
        }

        private void OpenLocalFile(string fileName)
        {
            FormMp4 newForm = new FormMp4();
            newForm.MdiParent = this;
            newForm.WindowState = FormWindowState.Maximized;
            //Set the stream file to the parser.
            newForm.SetStreamFile(fileName);
            newForm.Show();

            //Save into the MRU.
            ManagerMRU.AddMruItem(fileName);
        }

        private void FormMain_DragDrop(object sender, DragEventArgs e)
        {
            string[] fileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            foreach (string fileToParse in fileList)
            {
                OpenLocalFile(fileToParse);
            }
        }

        private void FormMain_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;
            else
                e.Effect = DragDropEffects.None;
        }
    }
}
